<template>
  <div class="wrapper">
    <!-- 头部 -->
    <header>
      <i class="fa fa-arrow-left" @click="goBack"></i>
      <div class="title">忘记密码</div>
      <div class="placeholder"></div>
    </header>

    <!-- 忘记密码表单 -->
    <div class="forgot-password-form">
      <!-- Logo -->
      <div class="logo">
        <img src="../assets/img/logo.png" alt="logo">
      </div>

      <!-- 表单输入区 -->
      <div class="form-group">
        <div class="input-group">
          <i class="fa fa-envelope"></i>
          <input 
            type="email" 
            v-model="email" 
            placeholder="请输入注册邮箱"
            @keyup.enter="handleForgotPassword"
          >
        </div>
      </div>

      <!-- 提交按钮 -->
      <div class="submit-btn" @click="handleForgotPassword">
        提交
      </div>

      <!-- 登录选项 -->
      <div class="options">
        <div class="login" @click="router.push('/login')">返回登录</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { forgotPassword } from '../api/user'

const router = useRouter()
const email = ref('')

const handleForgotPassword = async () => {
  try {
    if (!email.value) {
      ElMessage.warning('请输入注册邮箱')
      return
    }

    const res = await forgotPassword({ email: email.value })
    if (res.code === 0) {
      ElMessage.success('重置密码邮件已发送，请检查您的邮箱')
      router.push('/login')
    } else {
      ElMessage.error(res.msg || '操作失败')
    }
  } catch (error) {
    console.error('忘记密码操作失败:', error)
    ElMessage.error('操作失败，请稍后重试')
  }
}

// 返回到“我的”页面
const goBack = () => {
  router.push('/profile')
}
</script>

<style scoped>
.wrapper {
  width: 100%;
  min-height: 100vh;
  background-color: #fff;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3vw 4vw;
  background-color: #fff;
}

header .fa-arrow-left {
  font-size: 4vw;
  color: #ff7043;
  width: 8vw;
  cursor: pointer;
}

header .title {
  font-size: 4.5vw;
  font-weight: bold;
}

header .placeholder {
  width: 8vw;
}

.forgot-password-form {
  padding: 8vw 6vw;
}

.logo {
  text-align: center;
  margin-bottom: 8vw;
}

.logo img {
  width: 35vw;
  height: 35vw;
}

.form-group {
  margin-bottom: 6vw;
}

.input-group {
  display: flex;
  align-items: center;
  padding: 3vw 4vw;
  background-color: #f8f8f8;
  border-radius: 2vw;
  margin-bottom: 4vw;
}

.input-group i {
  font-size: 5vw;
  color: #ff7043;
  margin-right: 3vw;
}

.input-group input {
  flex: 1;
  border: none;
  background: none;
  font-size: 3.8vw;
  color: #333;
  outline: none;
}

.input-group input::placeholder {
  color: #999;
}

.submit-btn {
  background-color: #ff7043;
  color: #fff;
  text-align: center;
  padding: 3vw;
  border-radius: 2vw;
  font-size: 4vw;
  margin-bottom: 4vw;
  cursor: pointer;
}

.options {
  text-align: center;
  color: #666;
  font-size: 3.5vw;
}

.options > div {
  cursor: pointer;
}
</style> 